package cn.rlfit.gulimall.product.controller;

import cn.rlfit.common.utils.PageUtils;
import cn.rlfit.common.utils.R;
import cn.rlfit.common.validators.AddGroup;
import cn.rlfit.common.validators.UpdateGroup;
import cn.rlfit.common.validators.UpdateStatusGroup;
import cn.rlfit.gulimall.product.entity.BrandEntity;
import cn.rlfit.gulimall.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 品牌
 *
 * @author ruolifeng
 * @email sunruolifeng@gmail.com
 * @date 2023-02-03 12:35:35
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam() Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    public R info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     *
     * @Valid 告诉springMVC需要进行数据校验，
     * BindingResult bindingResult 获取到校验的结果
     */
    @RequestMapping("/save")
    public R save(@Validated(AddGroup.class) @RequestBody BrandEntity brand /*BindingResult bindingResult8*/) {
//        if (bindingResult.hasErrors()) {
////            获取校验的错误结果
//            Map<String, String> mapError = new HashMap<>();
//            bindingResult.getFieldErrors().forEach((item) -> {
////                获取到默认的错误提示消息
//                String message = item.getDefaultMessage();
////                错误字段名
//                String field = item.getField();
////                将每一个错误结果都保存在集合中
//                mapError.put(message, field);
//            });
//            return R.error(400, "提交的数据不合法").put("data", mapError);
//        } else {
//            brandService.save(brand);
//            return R.ok();
//        }
        brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@Validated(UpdateGroup.class) @RequestBody BrandEntity brand) {
        brandService.updateById(brand);

        return R.ok();
    }

    @RequestMapping("/update/status")
    public R updateStatus(@Validated(UpdateStatusGroup.class) @RequestBody BrandEntity brand) {
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     * RequestBod获取请求体数据进行删除，springMVC自动将请求体中的数据转换为对应的对象
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }
    @RequestMapping("/info/like/{key}")
    public R queryByKey(@PathVariable("key") String key){
        List<BrandEntity> brand = brandService.queryKey(key);
        return R.ok().put("brand", brand);
    }

}
